package org.freedoit.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import com.objectdb.o._NoResultException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.TypedQuery;
import org.apache.log4j.Logger;
import org.freedoit.common.util.SharedConstants;
import org.freedoit.exception.ServerException;
import org.freedoit.om.Project;
import org.freedoit.util.LogUtils;

/**
 * Home object for domain model class Project.
 * @see org.freedoit.om.Project
 * @author Hibernate Tools
 */
public class ProjectDAO {

	private static final Logger log = LogUtils.getLogger(ProjectDAO.class);

	public void persist(Project project) {
		log.debug("persisting Project instance");
		EntityManager em = DAOFactory.getEntityManagerFactory().createEntityManager();
		try {
			em.persist(project);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public void delete(Project project) {
		log.debug("deleting Project instance");
		EntityManager em = DAOFactory.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			project = em.find(Project.class, project.getSerialNo(), LockModeType.WRITE);
			em.remove(project);

			em.getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public void saveOrUpdate(Project project) {
		log.debug("updating Task instance");
		EntityManager em = DAOFactory.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();
            Project projectExisted = findByProjectId(project.getProjectId(), false);
            
            if (projectExisted != null) {
                project.setSerialNo(projectExisted.getSerialNo());
            }
			em.merge(project);
			em.getTransaction().commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public void update(Project project) {
		log.debug("updating Project instance");
		EntityManager em = DAOFactory.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();
			em.merge(project);
			em.getTransaction().commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public Project findByProjectId(Long projectId) {
        return findByProjectId(projectId, true);
    }

	public Project findByProjectId(Long projectId, boolean onlyActive) {
		log.debug("getting Project instance with id: " + projectId);
		EntityManager em = DAOFactory.getEntityManagerFactory().createEntityManager();
		try {
			
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("select from ").append(Project.class.getName());
			sbQuery.append(" t where t.projectId = ").append(projectId);
            
            if (onlyActive) {
                sbQuery.append(" and t.status = ").append(SharedConstants.ACTIVE_INTEGER);
            }
                        
            TypedQuery<Project> q = em.createQuery(sbQuery.toString(), Project.class);
			Project instance = q.getSingleResult();
			return instance;
		} catch (_NoResultException e) {
			log.error("Warning: no result find", e);
            //System.out.println("Warning: no result find");
            return null;
        } catch (RuntimeException re) {
			log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public Project findById(Long id) {
		log.debug("getting Project instance with id: " + id);
		EntityManager em = DAOFactory.getEntityManagerFactory().createEntityManager();
		try {
			Project instance = em.find(Project.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public List<Project> findAll() {
		EntityManager em = DAOFactory.getEntityManagerFactory().createEntityManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("select from ").append(Project.class.getName());
                        
            TypedQuery<Project> q = em.createQuery(sbQuery.toString(), Project.class);
			List<Project> results = q.getResultList();
			
			int iSize = 0;
			if (results != null) {
				iSize = results.size();
			}
			log.debug("find by user id successful, result size: " + iSize);
			return results;
		} catch (RuntimeException re) {
			log.error("find by user id failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}
}
